package com.molyfun.parents.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.classroom.ClassViewHolder;
import com.molyfun.parents.rest.bean.TeamListResponse;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 *我的班级
 */
public class MineClassRoomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;

    private static final int MINE_CLASS_HEADER = 1001;

    private static final int MINE_CLASS_BODY = 1002;

    private List<TeamListResponse.TeamBean> mList;

    private MainClassRoomInterface mMainClassRoomInterface;

    public MineClassRoomAdapter(Context context, MainClassRoomInterface mainClassRoomInterface) {
        this.mContext = context;
        mMainClassRoomInterface = mainClassRoomInterface;
    }

    public interface MainClassRoomInterface{
        void onUserInfoClick();
    }

    public void refreshData(List<TeamListResponse.TeamBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(mList != null){
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<TeamListResponse.TeamBean> list) {
        if(mList != null){
            this.mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case MINE_CLASS_HEADER:
                return new MineClassRoomHeaderViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.view_mine_class_room_header, parent, false));
            case MINE_CLASS_BODY:
            default:
                return new ClassViewHolder(LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_class_cell, parent, false));
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(position == 0){
            MineClassRoomHeaderViewHolder mineClassHeaderViewHolder = (MineClassRoomHeaderViewHolder)holder;
            mineClassHeaderViewHolder.mProfileIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mMainClassRoomInterface != null){
                        mMainClassRoomInterface.onUserInfoClick();
                    }
                }
            });

        }else{
            final TeamListResponse.TeamBean teamBean = mList.get(position - 1);
            ClassViewHolder classViewHolder = (ClassViewHolder)holder;

            if(position == getItemCount() - 1){
                classViewHolder.mClassView.initData(teamBean, true);
            }else{
                classViewHolder.mClassView.initData(teamBean);
            }
//            classViewHolder.mClassView.setPadding(mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0, mContext.getResources().getDimensionPixelOffset(R.dimen.common_normal_padding), 0);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return MINE_CLASS_HEADER;
        }else{
            return MINE_CLASS_BODY;
        }
    }


    public class MineClassRoomHeaderViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.profile_icon)
        ImageView mProfileIcon;

        public MineClassRoomHeaderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 1;
        }
        return mList.size() + 1;
    }

}
